package com.glodon.paas.monitor.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.WebUtils;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.monitor.server.biz.domain.MonitorUserEntity;
import com.glodon.paas.monitor.server.dto.MonitorResultMsg;
import com.glodon.paas.monitor.server.exposed.facade.MonitorAppUserFacadeService;
import com.glodon.paas.monitor.web.constants.MonitorWebPageConstants;

@Controller
public class LoginController {

	@Autowired
	private MonitorAppUserFacadeService	monitorAppUserFacadeService;

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public ModelAndView loginIndex(ModelMap model, HttpServletRequest request) {
		ModelMap doLoginModel = (ModelMap) request.getSession().getAttribute("doLoginModel");
		if (null != doLoginModel && !doLoginModel.isEmpty()) {
			model.addAttribute(StringConst.RETURN_MSG, doLoginModel.get(StringConst.RETURN_MSG));
			model.addAttribute(StringConst.RESULT, doLoginModel.get(StringConst.RESULT));
		}
		request.getSession().removeAttribute("doLoginModel");
		return new ModelAndView("login", model);
	}

	@RequestMapping(value = "doLogin", method = RequestMethod.POST)
	public ModelAndView doLogin(@RequestParam(value = "username", required = false) String username,
			@RequestParam(value = "password", required = false) String password, ModelMap model,
			HttpServletRequest request) {
		if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.LOGIN_USERNAME_PASSWORD_EMPTY);
			request.getSession().setAttribute("doLoginModel", model);
			return new ModelAndView(new RedirectView("login"));
		}

		MonitorResultMsg<MonitorUserEntity> userMsg = monitorAppUserFacadeService.loginByUserNameAndPassword(username,
				password);
		if (null == userMsg || !userMsg.isSuccess()) {
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);
			model.addAttribute(StringConst.RETURN_MSG, MonitorWebPageConstants.LOGIN_RESULT_INCORRECT);
			request.getSession().setAttribute("doLoginModel", model);
			return new ModelAndView(new RedirectView("login"));
		}

		MonitorUserEntity loginUser = (MonitorUserEntity) userMsg.getResultObject();
		WebUtils.setSessionAttribute(request, "loginUser", loginUser);
		request.getSession().setAttribute("doLoginModel", model);
		return new ModelAndView(new RedirectView("main"));
	}

	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public ModelAndView logout(ModelMap model, HttpServletRequest request) {
		request.getSession().removeAttribute("loginUser");
		return new ModelAndView("login", model);
	}
}
